import java.util.*;

public class Test2 {
    static int mod = 1_000_000_007;
    static int n;
    static List<Integer>[] graph;
    static int[] inDegree;
    static boolean[] used;
    static int ans = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        int[] a = new int[n + 1];
        graph = new ArrayList[n + 1];
        inDegree = new int[n + 1];
        used = new boolean[n + 1];

        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextInt();
            graph[i] = new ArrayList<>();
        }

        // 构建图
        for (int i = 1; i <= n; i++) {
            if (a[i] != i) {
                graph[i].add(a[i]);
                inDegree[a[i]]++;
            }
        }

        // 检查是否有环（用拓扑排序判断）
        if (hasCycle()) {
            System.out.println(0);
            return;
        }

        // 回溯计算拓扑排序数量
        dfs(0);
        System.out.println(ans);
    }

    // 回溯法枚举所有拓扑排序
    static void dfs(int depth) {
        if (depth == n) {
            ans = (ans + 1) % mod;
            return;
        }

        for (int i = 1; i <= n; i++) {
            if (!used[i] && inDegree[i] == 0) {
                // 选择这个节点
                used[i] = true;
                for (int nei : graph[i]) {
                    inDegree[nei]--;
                }

                dfs(depth + 1);

                // 回溯
                used[i] = false;
                for (int nei : graph[i]) {
                    inDegree[nei]++;
                }
            }
        }
    }

    // 判断图中是否有环
    static boolean hasCycle() {
        int[] deg = Arrays.copyOf(inDegree, n + 1);
        Queue<Integer> queue = new LinkedList<>();

        for (int i = 1; i <= n; i++) {
            if (deg[i] == 0) queue.offer(i);
        }

        int count = 0;
        while (!queue.isEmpty()) {
            int cur = queue.poll();
            count++;
            for (int nei : graph[cur]) {
                deg[nei]--;
                if (deg[nei] == 0) queue.offer(nei);
            }
        }
        return count != n;
    }
}
